package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.adapter.CrashRecordAdapter;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.BaseRecordBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/25.
 * 红包记录实现
 * coupon points vip   elec 红包 购物币  积分  现金币
 */

public class RecordRedPachetListActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.record_listView)
    ListView record_listView;

    private List<BaseRecordBean> listItems = new ArrayList<>();
    private CrashRecordAdapter crashRecordAdapter;
    private String type = "coupon";
    private int page = 0;
    private int pageNum = 10;


    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("现金红包记录");
        crashRecordAdapter = new CrashRecordAdapter(getBaseContext(),listItems);
        record_listView.setAdapter(crashRecordAdapter);

    }
    @Override
    public void initPresenter() {
        initViewRequest();

    }
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_record_list;
    }
    @OnClick(R.id.iv_back_page)
    public void onClickBack(){
        finish();
    }

    private void initViewRequest(){
        if(!NetWorkUtils.isNetConnected(getBaseContext())){
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .getUrlRequestAccountBill(UserUtils.getUserToken(), type, page, pageNum, new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            Gson gson = new Gson();
                            BaseRecordBean baseRecodeBean = gson.fromJson(responseBody.string(),BaseRecordBean.class);
                            if(baseRecodeBean.getError_code() == 0){
                                List<BaseRecordBean> forList = new ArrayList<>();
                               for(int i = 0; i< baseRecodeBean.getData().size();i++){
                                   forList.add(baseRecodeBean);
                               }
                               setData(forList);

                            }else if(baseRecodeBean.getError_code() == 3){

                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);

                            }else{
                                ToastUitl.showShort(baseRecodeBean.getError_message());
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                });

    }

    private void setData(List<BaseRecordBean> foreshowList) {

        for (BaseRecordBean vo : foreshowList) {
            listItems.add(vo);
        }
        crashRecordAdapter.notifyDataSetChanged();

    }
}
